package com.lc.pianostudent.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.lc.pianostudent.R;
import com.lc.pianostudent.conn.PostGetCode;
import com.lc.pianostudent.conn.PostResetPassword;
import com.lc.pianostudent.conn.SendResetPassword;
import com.lc.pianostudent.util.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilMatches;
import com.zcx.helper.util.UtilToast;
import com.zcx.helper.view.AppGetVerification;
import com.zcx.helper.view.AppUsername;
import com.zcx.helper.view.AppVerification;

/**
 * Created by Administrator on 2018/6/22.
 */

public class ForgetPassActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(value = R.id.forget_pass_next,isClick = true) private LinearLayout forget_pass_next;
    @BoundView(R.id.forget_phone) private AppUsername forget_phone;
    @BoundView(R.id.register_verification) private AppVerification register_verification;
    @BoundView(value = R.id.forget_pass_get_code,isClick = true) private AppGetVerification get_code;  //获取验证码
    private SendResetPassword.SendResetPasswordInfo info;
    //发送验证码接口
    public SendResetPassword sendResetPassword = new SendResetPassword(new AsyCallBack<SendResetPassword.SendResetPasswordInfo>(){


        @Override
        public void onSuccess(String toast, int type, Object o, SendResetPassword.SendResetPasswordInfo sendResetPasswordInfo) throws Exception {

                        countTime.start();
            info = sendResetPasswordInfo;
            UtilToast.show(toast);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    //忘记密码接口
    private PostResetPassword postResetPassword = new PostResetPassword(new AsyCallBack<PostResetPassword.PostResetPasswordInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostResetPassword.PostResetPasswordInfo postResetPasswordInfo) throws Exception {
            if (postResetPasswordInfo.code.equals("200")) {
                UtilToast.show(toast);


                startActivity(new Intent(ForgetPassActivity.this,ForgetNextActivity.class).putExtra("phone",forget_phone.getText().toString().trim()));
            } else {
                UtilToast.show(toast);
            }
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(toast);

        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pass);

        setBackTrue();
        initView();
    }

    private void initView(){
        setTitleName("忘记密码");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.forget_pass_next:

                String phone = forget_phone.getText().toString().trim();
                String code = register_verification.getText().toString().trim();

                if (TextUtils.isEmpty(phone)) {
                    UtilToast.show("请输入手机号");
                } else if (!Validator.isMobile(phone)) {
                    UtilToast.show("请输入正确手机号");
                } else if (TextUtils.isEmpty(code)) {
                    UtilToast.show("请输入验证码");
                } else {
                    postResetPassword.username = phone;
                    postResetPassword.sms_code = code;
                    postResetPassword.execute();
                }

                break;

            //获取验证码
            case R.id.forget_pass_get_code:

                if (TextUtils.isEmpty(forget_phone.getText().toString().trim())) {
                    UtilToast.show("请输入手机号");
                } else if (!UtilMatches.checkMobile(forget_phone.getText().toString().trim())) {
                    UtilToast.show("请输入正确手机号");
                } else {
                    sendResetPassword.username = forget_phone.getText().toString().trim();

                    sendResetPassword.execute();
                }

                break;
        }
    }

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {
                ScaleScreenHelperFactory.getInstance().loadViewSize(get_code, 24);
                get_code.setText(millisUntilFinished / 1000 + "s后重新获取");
                get_code.setEnabled(false);
            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                ScaleScreenHelperFactory.getInstance().loadViewSize(get_code, 24);
                get_code.setText("重新获取验证码");
                get_code.setEnabled(true);
            }
        }
    };
}
